﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Anata;
using Anata.AnataServerDataSetTableAdapters;

namespace Anata.TableForm
{
    public partial class FreeGiftForm : Form
    {
        AdminDialogueForm _f;
        free_giftTableAdapter freegift = new free_giftTableAdapter();
        AnataServerDataSet.free_giftDataTable dt = new AnataServerDataSet.free_giftDataTable();

        Log Log = new Log();
        _log l;
        List<_log> list = new List<_log>();

        DataRowCollection dr;
        DataRow drow;

        bool isUpdate = false;
        int id;

        public FreeGiftForm() { }

        public FreeGiftForm(AdminDialogueForm f)
        {
            InitializeComponent();
            _f = f;

            comboBox2.SelectedIndex = 0;

            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form_FormClosing);
        }

        public FreeGiftForm(AdminDialogueForm f, DataGridView dataGridView1)
        {
            InitializeComponent();
            _f = f;
            isUpdate = true;
            id = Convert.ToInt32(dataGridView1[0, dataGridView1.SelectedCells[0].RowIndex].Value);

            dt = freegift.GetDataByID(id);
            dr = dt.Rows;
            drow = dr[0];

            comboBox1.SelectedValue = drow[dt.id_layanan_cabangColumn].ToString();
            textBox1.Text = drow[dt.namaColumn].ToString();
            textBox2.Text = drow[dt.kuantitasColumn].ToString();
            if (drow[dt.tidak_aktifColumn].ToString().Equals("0")) comboBox2.SelectedIndex = 0; else comboBox2.SelectedIndex = 1;

            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form_FormClosing);
        }

        private void Form_FormClosing(object sender, FormClosingEventArgs e)
        {
            _f.PerformRefresh();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void insertData_Click(object sender, EventArgs e)
        {
            if (isDataValid())
            {
                if (isUpdate)
                {
                    if (comboBox1.DroppedDown)
                    {
                        l = new _log("free_gift", id, "id_layanan_cabang", "edit", drow[dt.id_layanan_cabangColumn].ToString(), comboBox1.SelectedValue.ToString());
                        list.Add(l);
                    }
                    if (textBox1.Modified)
                    {
                        l = new _log("free_gift", id, "nama", "edit", drow[dt.namaColumn].ToString(), textBox1.Text);
                        list.Add(l);
                    }
                    if (textBox2.Modified)
                    {
                        l = new _log("free_gift", id, "kuantitas", "edit", drow[dt.kuantitasColumn].ToString(), textBox2.Text);
                        list.Add(l);
                    }
                    if (comboBox2.DroppedDown)
                    {
                        l = new _log("free-gift", id, "tidak_aktif", "edit", drow[dt.tidak_aktifColumn].ToString(), comboBox1.SelectedIndex == 0 ? "false" : "true");
                        list.Add(l);
                    }
                    try
                    {
                        freegift.UpdateQuery(Convert.ToInt32(comboBox1.SelectedValue),
                            textBox1.Text,
                            Convert.ToInt32(textBox2.Text),
                            comboBox1.SelectedIndex == 0 ? "false" : "true",
                            id);
                    }
                    catch (Exception msg) { MessageBox.Show("Update tidak berhasil, periksa kembali input yang anda masukkan."); }
                    this.Close();
                }
                else
                {
                    try
                    {
                        freegift.Insert(Convert.ToInt32(comboBox1.SelectedValue),
                            textBox1.Text,
                            Convert.ToInt32(textBox2.Text),
                            comboBox1.SelectedIndex == 0 ? "false" : "true");
                    }
                    catch (Exception msg) { MessageBox.Show("Input tidak berhasil, periksa kembali input yang anda masukkan."); }
                    this.Close();
                }
                Log.ProcessLog(list);
            }
            else { MessageBox.Show("Data yang diperlukan belum lengkap."); }
        }

        public bool isDataValid()
        {
            if (!textBox1.Text.Equals("") &&
                !textBox2.Text.Equals(""))
            { return true; }
            else { return false; }
        }

        private void FreeGiftForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'anataServerDatabase.metode_pembayaran' table. You can move, or remove it, as needed.
            this.metode_pembayaranTableAdapter.Fill(this.anataServerDatabase.metode_pembayaran);
            // TODO: This line of code loads data into the 'anataServerDatabase.layanan_x_cabang' table. You can move, or remove it, as needed.
            this.layanan_cabangTableAdapter.Fill(this.anataServerDatabase.layanan_cabang);
        }
    }
}

